package com.wsx.ones.boots.actor;


import akka.actor.UntypedAbstractActor;
import com.wsx.ones.boots.bean.Message;
import com.wsx.ones.boots.service.BussinesService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.concurrent.CompletableFuture;

/**
 * Created by wangshuaixin on 18/6/15.
 */
@Component("workerActor")
@Scope("prototype")
public class WorkerActor extends UntypedAbstractActor {


    @Autowired
    private BussinesService bussinesService;


    private final CompletableFuture<Message> future;

    public WorkerActor(CompletableFuture<Message> future) {
        this.future = future;
    }

    @Override
    public void onReceive(Object o) throws Throwable {
        if (o instanceof Message) {
            Message message = (Message) o;
            bussinesService.resetMessage(message);
            future.complete(message);
        } else {
            unhandled(o);
        }

        getContext().stop(getSelf());
    }
}
